import Vue from 'vue'
import VueRouter from 'vue-router'
import StudentLayout from "@/components/student/StudentLayout";
import RecentTable from "@/components/student/mianView/RecentGrade";
import SelectGrade from "@/components/student/mianView/SelectGrade";
import StudentHome from "@/components/student/mianView/Home";
import TeacherLayout from "@/components/teacher/TeacherLayout";
import GradeManger from "@/components/teacher/mianView/gradeManger/GradeManger";
import StudentManger from "@/components/teacher/mianView/studentList/StudentManger";
import TeacherHome from "@/components/teacher/mianView/Home";
import AddStudentView from "@/components/teacher/mianView/addStudent/AddStudentView";


Vue.use(VueRouter)

const routes = [
    {
        path: '/student/:sid', component: StudentLayout,props: true,
        children: [
            {
                path: 'recent_table', component: RecentTable
            },
            {
                path: 'select_grade', component: SelectGrade
            },
            {
                path: 'home', component: StudentHome
            },
            {
                path: '/', redirect: 'home'
            }
        ]
    },
    {
        path: '/teacher/:tid', component: TeacherLayout, props: true,
        children: [
            {
                path: 'grade_manger', component: GradeManger
            },
            {
                path: 'student_list', component: StudentManger
            },
            {
                path: 'home', component: TeacherHome
            },
            {
                path:'add_student',component: AddStudentView
            },
            {
                path: '/', redirect: 'student_list'
            }
        ]
    },
    {
        path: '/login',
        component: () => import('../components/Login')
    },
    {
        path: '/',
        redirect: '/login'
    },

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
//挂载路由导航首位
//
router.beforeEach((to, from, next) => {
    const tk = window.sessionStorage.getItem('token')
    if (to.path === '/login') {
        if (tk) {
            return next('/home')
        } else {
            return next()
        }
    }
    if (!tk) return next('/login')
    next()
})


export default router
